// hyb_yl/gerenxinxi/gerenxinxi.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  onsubmit: function (e) {
    var that = this
    var values = e.detail.value;
    var openid = wx.getStorageSync('openid')
    var age = values.age
    var gender = values.gender
    var miaoshu = values.miaoshu
    var yibao = values.yibao
    var idcad = values.idcad
    var tel = values.tel
    var user = values.user
    var my_id = values.my_id
    console.log(my_id)
    if (values.user == "") {
      wx.showModal({
        content: '姓名不能为空',
      })
    } else if (values.age == "") {
      wx.showModal({
        content: '请填写您的年龄',
      })
    } else if (values.tel == "点击验证手机号") {
      that.setData({
        title: '请填前往个人中心验证手机号',
        close: true
      })
      setTimeout(function () {
        that.setData({
          close: false
        })
      }, 2000)
    } else if (values.idcad == "" || values.idcad.length < 18) {
      wx.showModal({
        content: '请正确填写您的身份证号',
      })
    } else if (values.my_id !== '') {
      app.util.request({
        'url': "entry/wxapp/Myinfors",
        data: {
          my_id: my_id,
          age: age,
          gender: gender,
          miaoshu: miaoshu,
          tel: tel,
          user: user,
          yibao: yibao,
          idcad: idcad,
          openid: openid
        },
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 800,
            success: function () {
              wx.reLaunch({
                url: '/hyb_yl/tabBar/my/my'
              })
            }, //接口调用成功的回调函数 
          })

        },
        fail: function (err) {
          console.log(err)
        },
      })
    } else {
      app.util.request({
        'url': "entry/wxapp/Myinfors",
        data: {
          my_id: my_id,
          age: age,
          gender: gender,
          miaoshu: miaoshu,
          tel: tel,
          user: user,
          yibao: yibao,
          idcad: idcad,
          openid: openid
        },
        success: function (res) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 800,
            success: function () {
              if (that.data.types == 1) {
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.setData({
                  types: 2
                })
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.reLaunch({
                  url: '/hyb_yl/tabBar/my/my'
                })
              }
            }, //接口调用成功的回调函数 
          })
        },
        fail: function (err) {
          console.log(err)
        },
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this
    var openid = wx.getStorageSync('openid')
    var uniacid = app.siteInfo.uniacid;

    if (options.types) {
      that.setData({
        types: options.types
      })
    }
    app.util.request({
      'url': "entry/wxapp/Myinforsarray",
      data: {
        openid: openid,
        uniacid: uniacid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          myinforsarray: res.data.data
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getMyphone()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMyphone()
    var that = this
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    console.log(currPage.data.myphone)
    if (currPage.data.myphone !== "" && currPage.data.myphone !== undefined) {
      that.setData({
        myinforphone: currPage.data.myphone
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getMyphone: function () {
    var that = this
    var openid = wx.getStorageSync('openid')
    app.util.request({
      'url': "entry/wxapp/Myinforphone",
      data: {
        openid: openid,
      },
      success: function (res) {
        //console.log(res)
        that.setData({
          myinforphone: res.data.data.u_phone
        })
      },
      fail: function (err) {
        console.log(err)
      },
    })
  },
  tiaozhuan: function () {
    wx.navigateTo({
      url: '/hyb_yl/mysubpages/pages/tel/tel?enter=1',
    })
  }
})